﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Text;

public class Handler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        //context.Response.ContentType = "text/plain";
        int useridx = MediaUtil.ReturnMemberId();
        string userid = MediaUtil.ReturnUserID();
        string action = string.Empty;
        string output = string.Empty;
        if (context.Request["action"] != null && context.Request["action"].Length > 0)
        {
            action = context.Request["action"].ToString();
        }
        else
        {
            return;
        }
        //useridx = 18464630;
        //userid = "wuxinjie0515";
        if (useridx <= 0)
        {
            return;
        }

        if (action == "lottery")
        {
            
            if (DateTime.Now >= DateTime.Parse("2013-02-26"))
            {
                output = "[{\"ret\":" + "-4" + "}]";
            }
            else
            {
                int ret = lottery(1,useridx,userid);
                output = "[{\"ret\":" + ret.ToString() + "}]";
            }
        }
        else if (action == "rank")
        {
            DataSet ds = active.List(9);

            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                output = active.CreateJson(ds.Tables[0]);
            }
        }
        else if (action == "win")
        {
            int ret = lottery(2,useridx,userid);

            output = "[{\"ret\":" + ret.ToString() + "}]";
        }
        else if (action == "user")
        {
            DataTable dt =active.UserInfo(useridx);
            if (dt != null && dt.Rows.Count > 0)
            {
                output = active.CreateJson(dt);

            }  
        }
        context.Response.Write(output);
    }





    /// <summary>
    /// 抽奖
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static int lottery(int type, int useridx, string userid)
    {
        int ret = -2;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,50,userid),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)                  
                    };
        try
        {
            SqlHelper.ExecuteNonQuery(active.Conn, CommandType.StoredProcedure, "[pr_yuanxiao2013]", p);
            ret = int.Parse(p[3].Value.ToString());
        }
        catch
        {

        }
        return ret;
    }
    
 
    
    public bool IsReusable
    {
        get {
            return false;
        }
    }

}